package com.controller.community;


import com.alibaba.fastjson.JSONObject;
import com.entity.Area;
import com.utils.BaseController;
import com.utils.DataTables;
import com.entity.HousingEstate;
import com.service.AreaService;
import com.service.HousingEstateService;
import com.service.Impl.AreaServiceImpl;
import com.service.Impl.HousingEstateServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 小区管理 Controller
 */
@Controller
@RequestMapping("/housing_estate")
public class CommunityController extends BaseController{

    @Autowired
    private AreaService areaService;

    @Autowired
    HttpServletRequest request;//请求
    @Autowired
    private HousingEstateService housingEstateService;

    /**
     * 分页查询小区列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getPageList.do", produces = "application/json; charset=utf-8")
    public String getPageList() {
        return JSONObject.toJSONString(housingEstateService.
                getPageList(DataTables.getInstance(request, null)));
    }

    /**
     * 获取小区详细信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/selectHousingEstateById.do", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String selectHousingEstateById(Integer id) {
        HousingEstate housingEstate = housingEstateService.selectHousingEstateById(id);
//        System.out.println(housingEstate.getCityName()+housingEstate.getProvinceName());
        String ss=housingEstate.getProvinceName()+housingEstate.getCityName();
        housingEstate.setSs(ss);
        return JSONObject.toJSONString(housingEstate);
    }

    /**
     * 添加小区信息
     *
     * @param housingEstate
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add.do", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String addHousingEstate(@ModelAttribute HousingEstate housingEstate) {
        Map<String, Object> map = housingEstateService.addHousingEstate(housingEstate);
        return JSONObject.toJSONString(map);
    }

    /**
     * 获取所有省市信息
     * @return
     */
    @RequestMapping(value = "/getAreas.do", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String getAreas(){
        return JSONObject.toJSONString(areaService.selectArea());
    }

    /**
     * 修改小区信息
     * @return
     */
    @RequestMapping(value = "/update.do", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String updateHousingEstate(@ModelAttribute HousingEstate housingEstate){
        return JSONObject.toJSONString(housingEstateService.updateHousingEstate(housingEstate));
    }

    /**
     * 批量删除小区信息
     * @param idlist
     * @return
     */
    @RequestMapping(value = "/del.do", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String delHousingEstate(@RequestParam("idlist[]") List<Integer>idlist){
//        System.out.println("开始批量删除");
        return  JSONObject.toJSONString(housingEstateService.delHousingEstate(idlist));

    }
}
